import machine_control
import snail_fun
import logging
from shared import stop_event

snail = snail_fun.Snail()

def reaction(inject_steps):
    logging.info("开始反应流程")
    # 按 index 顺序遍历
    for step in sorted(inject_steps, key=lambda x: x["index"]):
        port = step["reagent"]
        times = step["cycles"]
        timewait = step["timewait"]
        temperature = step["temperature"]
        idx = step["index"]
        logging.info(f"步骤{idx}: 端口{port}, 循环{times}次, 等待{timewait}s, 温度{temperature}°C")
        
        for i in range(times):
            if stop_event.is_set():
                logging.warning("检测到停止标志，终止 reaction")
                return
                
            snail.inject(
                reagent=port,   # 端口号
                times=1,        # 每次只注入一次
                timewait=timewait,                         
                temperature=temperature,
            )
    
    logging.info("reaction() 完成")


if __name__ == "__main__":
    ma = machine_control
    inject_steps = [
                        {
                            "index": 1,
                            "reagent": 3,
                            "cycles": 1,
                            "timewait": 10,
                            "temperature": 25
                        },
                        {
                            "index": 2,
                            "reagent": 4,
                            "cycles": 1,
                            "timewait": 600,
                            "temperature": 25
                        },
                        {
                            "index": 3,
                            "reagent": 5,
                            "cycles": 1,
                            "timewait": 10,
                            "temperature": 25
                        },
                        {
                            "index": 4,
                            "reagent": 3,
                            "cycles": 1,
                            "timewait": 10,
                            "temperature": 25
                        },
                        {
                            "index": 5,
                            "reagent": 6,
                            "cycles": 1,
                            "timewait": 300,
                            "temperature": 25
                        },
                        {
                            "index": 6,
                            "reagent": 9,
                            "cycles": 1,
                            "timewait": 10,
                            "temperature": 25
                        },
                        {
                            "index": 7,
                            "reagent": 7,
                            "cycles": 1,
                            "timewait": 600,
                            "temperature": 25
                        },
                        {
                            "index": 8,
                            "reagent": 9,
                            "cycles": 2,
                            "timewait": 10,
                            "temperature": 25
                        },
                        {
                            "index": 9,
                            "reagent": 8,
                            "cycles": 1,
                            "timewait": 10,
                            "temperature": 25
                        }
                    ]
    ma.reaction(inject_steps)


